package org.sanelli.InterstellarGolf.GenericObject;

public class Vector2D {

	private double x;
	private double y;
	
	public Vector2D() {
		this.x=0.0d;
		this.y=0.0d;
	}
	
	public Vector2D(double x, double y) {
		this.x=x;
		this.y=y;
	}
	
	public void setX(double x) {
		this.x = x;
	}
	
	public double getX() {
		return x;
	}
	
	public void setY(double y) {
		this.y = y;
	}
	
	public double getY() {
		return y;
	}
	
	public void sub(Vector2D v) {
		this.x -= v.getX();
		this.y -= v.getY();
	}
	
	public static Vector2D sub(Vector2D a, Vector2D b) {
		return new Vector2D(a.getX()-b.getX(), a.getY()-b.getY());
	}
	
	public void sub(double x, double y) {
		this.x -= x;
		this.y -= y;
	}
	
	public void add(Vector2D v) {
		this.x += v.getX();
		this.y += v.getY();
	}
	
	public static Vector2D add(Vector2D a, Vector2D b) {
		return new Vector2D(a.getX()+b.getX(), a.getY()+b.getY());
	}
	
	public static Vector2D mul(Vector2D a, Vector2D b) {
		return new Vector2D(a.getX()*b.getX(), a.getY()*b.getY());
	}
	
	public Vector2D normalize() {
		double z = this.magnitude();
		return new Vector2D( this.x/z, this.y/z);
	}
	
	public void mul(double d) {
		this.x *= d;
		this.y *= d;
	}
	
	public double magnitude() {
		return Math.sqrt(x*x + y*y);
	}
	
	public double GetAngle() {
		if (x > 0 && y >= 0) {
			return Math.atan(y / x);
		}
		if (x > 0 && y < 0) {
			return Math.atan(y / x) + 2 * Math.PI;
		}
		if (x < 0) {
			return Math.atan(y / x) + Math.PI;
		}
		if (x == 0 && y > 0) {
			return Math.PI / 2.0d;
		} else { // (x==0 && y<0) {
			return -Math.PI / 2.0d;
		}
	}
	
	public static Vector2D rotate(Vector2D v, float angle) {
		return new Vector2D((float) (v.x * Math.cos(angle) - v.y
				* Math.sin(angle)), (float) (v.x * Math.sin(angle) + v.y
				* Math.cos(angle)));
	}
	
	@Override
	public String toString() {
		return " X "+this.getX()+" Y "+this.getY();
	}
	
	public Vector2D copyVect() {
		return new Vector2D(this.x, this.y);
	}
}
